package com.koushikdutta.ion.conscrypt;

import android.content.Context;
import com.koushikdutta.async.AsyncSSLSocketWrapper;
import com.koushikdutta.async.future.Cancellable;
import com.koushikdutta.async.http.AsyncHttpClientMiddleware.GetSocketData;
import com.koushikdutta.async.http.AsyncSSLSocketMiddleware;
import com.koushikdutta.async.http.SimpleMiddleware;
import java.security.Provider;
import java.security.Security;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

public class ConscryptMiddleware extends SimpleMiddleware {
    static boolean initialized;
    static final Object lock = new Object();
    static boolean success;
    Context context;
    boolean enabled = true;
    boolean instanceInitialized;
    AsyncSSLSocketMiddleware middleware;

    static void initialize(Context context) {
        try {
            synchronized (lock) {
                if (initialized) {
                    return;
                }
                initialized = true;
                if (Security.getProvider("GmsCore_OpenSSL") != null) {
                    return;
                }
                SSLContext originalDefaultContext = SSLContext.getDefault();
                SSLSocketFactory originalDefaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
                context.createPackageContext("com.google.android.gms", 3).getClassLoader().loadClass("com.google.android.gms.common.security.ProviderInstallerImpl").getMethod("insertProvider", new Class[]{Context.class}).invoke(null, new Object[]{context});
                Provider[] providers = Security.getProviders();
                Provider provider = Security.getProvider("GmsCore_OpenSSL");
                Security.removeProvider("GmsCore_OpenSSL");
                Security.insertProviderAt(provider, providers.length);
                SSLContext.setDefault(originalDefaultContext);
                HttpsURLConnection.setDefaultSSLSocketFactory(originalDefaultSSLSocketFactory);
                success = true;
            }
        } catch (Exception e) {
        }
    }

    public void initialize() {
        initialize(this.context);
        if (success && !this.instanceInitialized && this.enabled) {
            this.instanceInitialized = true;
            SSLContext sslContext = null;
            try {
                sslContext = SSLContext.getInstance("TLS", "GmsCore_OpenSSL");
            } catch (Exception e) {
            }
            if (sslContext == null) {
                try {
                    sslContext = SSLContext.getInstance("TLS");
                } catch (Exception e2) {
                    return;
                }
            }
            sslContext.init(null, null, null);
            if (this.middleware.getSSLContext() != AsyncSSLSocketWrapper.getDefaultSSLContext()) {
                this.middleware.setSSLContext(sslContext);
            }
        }
    }

    public ConscryptMiddleware(Context context, AsyncSSLSocketMiddleware middleware) {
        this.middleware = middleware;
        this.context = context.getApplicationContext();
    }

    public Cancellable getSocket(GetSocketData data) {
        if (!this.enabled) {
            return null;
        }
        initialize();
        return super.getSocket(data);
    }
}
